package com.soxing.braindata.service.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.soxing.braindata.dao.entity.Research;
import com.soxing.braindata.dao.service.IResearchService;
import com.soxing.braindata.domain.enumeration.FunTypeEnum;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginSection;
import com.soxing.braindata.domain.rpc.OriginSectionRequest;
import com.soxing.braindata.domain.vo.VpInfoVo;
import com.soxing.common.utils.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 同步方案(同步访视编码)
 */
@Slf4j
@Service("syncSectionService")
public class SyncSectionServiceImpl implements ISyncService<OriginSectionRequest> {

    @Resource
    private IResearchService researchService;

    @Override
    public SyncResponse syncData(OriginSectionRequest request) {
        try{
            Integer funType = request.getFunType();
            //todo
            String subjectCode = request.getSubjectCode();
            List<OriginSection> programList = request.getProgramList();
            if (funType == null || subjectCode == null) {
                log.error("funType或subjectPkId为空。funType:{},subjectPkId:{}", funType, subjectCode);
                return SyncResponse.fail("funType或subjectPkId为空");
            }

            if (funType == FunTypeEnum.DELETE.getFunType()) {
                return SyncResponse.success();
            }
            if (CollectionUtils.isEmpty(programList)) {
                return SyncResponse.fail("方案列表为空");
            }
            Collections.sort(programList, new Comparator<OriginSection>() {
                @Override
                public int compare(OriginSection o1, OriginSection o2) {
                    if (o1.getShowOrder() > o2.getShowOrder()) {
                        return 1;
                    } else if (o1.getShowOrder() < o2.getShowOrder()) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });

            List<VpInfoVo> vpInfoVos = Lists.newArrayList();
            programList.forEach(program -> {
                VpInfoVo vpInfoVo = new VpInfoVo();
                vpInfoVo.setName(program.getSectionName());
                String mappedKey = program.getMappedKey();
                String[] sArr = mappedKey.split("_");
                if (sArr.length <= 1) {
                    vpInfoVo.setCode("V0");
                } else {
                    vpInfoVo.setCode(sArr[1]);
                }
                vpInfoVo.setOrder(program.getShowOrder());

                vpInfoVos.add(vpInfoVo);
            });

            Research research = new Research();
            research.setVpInfo(ObjectUtil.toJson(vpInfoVos));
            researchService.update(research, new LambdaQueryWrapper<Research>().eq(Research::getSubjectCode, subjectCode));
            return SyncResponse.success();


        }catch (Exception e)
        {
            log.error("同步访视服务异常", e);
            return SyncResponse.fail();
        }

    }


}
